package com.liaogui.oss;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

/**
 *
 * @author liaogui
 * @date 2025/1/21 20:42
 */
@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
public class MinIOFileController {
    private final S3Client s3Client;

    @Value("${s3.bucket}")
    private String bucket;

    @PostMapping
    public String createBucket(String bucketName) {
        String policy = "{\n" +
                "    \"Version\": \"2012-10-17\",\n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": {\n" +
                "                \"AWS\": [\n" +
                "                    \"*\"\n" +
                "                ]\n" +
                "            },\n" +
                "            \"Action\": [\n" +
                "                \"s3:GetBucketLocation\",\n" +
                "                \"s3:ListBucketMultipartUploads\"\n" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::liaogui\"\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": {\n" +
                "                \"AWS\": [\n" +
                "                    \"*\"\n" +
                "                ]\n" +
                "            },\n" +
                "            \"Action\": [\n" +
                "                \"s3:DeleteObject\",\n" +
                "                \"s3:GetObject\",\n" +
                "                \"s3:ListMultipartUploadParts\",\n" +
                "                \"s3:PutObject\",\n" +
                "                \"s3:AbortMultipartUpload\"\n" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::liaogui/*\"\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        // 创建存储桶
        CreateBucketResponse createBucketResponse = s3Client.createBucket(
                CreateBucketRequest.builder().bucket(bucketName).build()
        );
        System.out.println(createBucketResponse);
        PutBucketPolicyResponse putBucketPolicyResponse = s3Client.putBucketPolicy(PutBucketPolicyRequest.builder()
                .bucket(bucket)
                .policy(policy)
                .build());
        System.out.println(putBucketPolicyResponse);

        return "创建桶成功";
    }

    @DeleteMapping("/{bucketName}")
    public DeleteBucketResponse deleteBucket(@PathVariable("bucketName") String bucketName) {
        // 删除存储桶
        return s3Client.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
    }


}
